package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.AlbumInfoService;
import com.atguigu.tingshu.common.cache.GuiGuCache;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.query.album.AlbumInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumInfoVo;
import com.atguigu.tingshu.vo.album.AlbumListVo;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.OAuthFlow;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.yaml.snakeyaml.events.Event;

import java.util.List;

@Tag(name = "专辑管理")
@RestController
@RequestMapping("api/album")
@SuppressWarnings({"all"})
public class AlbumInfoApiController {

    @Autowired
    private AlbumInfoService albumInfoService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * /api/album/albumInfo/findUserAllAlbumList
     * TODO 该接口需要登录方可访问
     * 获取当前用户专辑列表
     * @return
     */
    @Operation(summary = "获取当前用户全部专辑列表")
    @GetMapping("/albumInfo/findUserAllAlbumList")
    @GuiGuLogin(required = true)
    public Result<List<AlbumInfo>> findUserAllAlbumList() {

        //	调用服务层方法
        List<AlbumInfo> list = albumInfoService.findUserAllAlbumList(AuthContextHolder.getUserId());
        return Result.ok(list);
    }


    /**
     * api/album/albumInfo/updateAlbumInfo/{id}
     * 修改专辑
     *
     * @param id
     * @param albumInfoVo
     * @return
     */
    @SneakyThrows
    @Operation(summary = "修改专辑")
    @PutMapping("/albumInfo/updateAlbumInfo/{id}")
    public Result updateAlbumInfo(@PathVariable Long id, @RequestBody @Validated AlbumInfoVo albumInfoVo) {

        //延迟双删+过期时间
        redisTemplate.delete(RedisConstant.ALBUM_INFO_PREFIX+id);

        //过期时间
        Thread.sleep(500);
        //调用service
        albumInfoService.updateAlbumInfo(id, albumInfoVo);

        //再次删除
        redisTemplate.delete(RedisConstant.ALBUM_INFO_PREFIX+id);
        return Result.ok();
    }


    /**
     * api/album/albumInfo/getAlbumInfo/{id}
     * 根据ID查询专辑信息
     *
     * @param id
     * @return
     */
    @SneakyThrows
    @GuiGuCache(prefix = RedisConstant.ALBUM_INFO_PREFIX)
    @Operation(summary = "根据ID查询专辑信息")
    @GetMapping("/albumInfo/getAlbumInfo/{id}")
    public Result<AlbumInfo> getAlbumInfo(@PathVariable Long id) {

        //延迟双删+过期时间
        redisTemplate.delete(RedisConstant.ALBUM_INFO_PREFIX+id);

        //过期时间
        Thread.sleep(500);
        AlbumInfo albumInfo = albumInfoService.getAlbumInfoById(id);
        redisTemplate.delete(RedisConstant.ALBUM_INFO_PREFIX+id);

        return Result.ok(albumInfo);
    }


    /**
     * api/album/albumInfo/removeAlbumInfo/{id}
     * 根据ID删除专辑
     *
     * @param id
     * @return
     */
    @SneakyThrows
    @DeleteMapping("/albumInfo/removeAlbumInfo/{id}")
    @Operation(summary = "根据ID删除专辑")
    public Result removeAlbumInfo(@PathVariable Long id) {

        redisTemplate.delete(RedisConstant.ALBUM_INFO_PREFIX+id);
        Thread.sleep(500);
        albumInfoService.removeAlbumInfo(id);
        redisTemplate.delete(RedisConstant.ALBUM_INFO_PREFIX+id);
        return Result.ok();
    }

    /**
     * api/album/albumInfo/findUserAlbumPage/{page}/{limit}
     * 查看当前用户专辑分页列表
     *
     * @param page
     * @param limit
     * @param albumInfoQuery
     * @return
     */
    @PostMapping("/albumInfo/findUserAlbumPage/{page}/{limit}")
    @Operation(summary = "查看当前用户专辑分页列表")
    @GuiGuLogin(required = true)
    public Result<Page<AlbumListVo>> findUserAlbumPage(@PathVariable Long page,
                                                       @PathVariable Long limit,
                                                       @RequestBody AlbumInfoQuery albumInfoQuery) {

        //获取用户id
        Long userId = AuthContextHolder.getUserId();
        //封装用户ID
        albumInfoQuery.setUserId(userId);

        //封装分页查询对象
        Page<AlbumListVo> albumListVoPage = new Page<>(page, limit);
        //执行查询
        albumListVoPage = albumInfoService.findUserAlbumPage(albumListVoPage, albumInfoQuery);

        return Result.ok(albumListVoPage);
    }


    /**
     * 新增专辑
     * api/album/albumInfo/saveAlbumInfo
     *
     * @return
     */
    @PostMapping("/albumInfo/saveAlbumInfo")
    @Operation(summary = "新增专辑")
    @GuiGuLogin(required = true)
    public Result saveAlbumInfo(@RequestBody @Validated AlbumInfoVo albumInfoVo) {

        //获取用户id
        Long userId = AuthContextHolder.getUserId();
        //执行保存专辑
        albumInfoService.saveAlbumInfo(userId, albumInfoVo);

        return Result.ok();

    }

    /**
     * 根据专辑ID获取专辑统计信息
     *
     * @param albumId
     * @return
     */
    @SneakyThrows
    @Operation(summary = "根据专辑ID获取专辑统计信息")
    @GetMapping("/albumInfo/getAlbumStatVo/{albumId}")
    public Result<AlbumStatVo> getAlbumStatVo(@PathVariable Long albumId) {
        redisTemplate.delete(RedisConstant.ALBUM_INFO_PREFIX+albumId);
        Thread.sleep(500);
        AlbumStatVo statVo = albumInfoService.getAlbumStatVo(albumId);
        redisTemplate.delete(RedisConstant.ALBUM_INFO_PREFIX+albumId);
        return Result.ok(statVo);
    }

}
